40个简单而有效的Shell脚本,想不起来的时候一看就懂(下)
21.bash中的函数
#!/bin/bash
function Add()
{
echo -n "Enter a Number: "
read x
echo -n "Enter another Number: "
read y
echo "Adiition is: $(( x+y ))"
}
Add
22.有返回值的函数
#!/bin/bash
function Greet() {
str="Hello $name, what brings you to UbuntuPit.com?"
echo $str
}
echo "-> what's your name?"
read name
val=$(Greet)
echo -e "-> $val"
23.用bash脚本创建目录
#!/bin/bash
echo -n "Enter directory name ->"
read newdir
cmd="mkdir $newdir"
eval $cmd
`mkdir $newdir`
24.确认存在后创建目录
#!/bin/bash
echo -n "Enter directory name ->"
read dir
if [ -d "$dir" ]
then
echo "Directory exists"
else
`mkdir $dir`
echo "Directory created"
fi
25.读取文件
1. Vim
2. Emacs
3. ed
4. nano
5. Code
#!/bin/bash
file='editors.txt'
while read line; do
echo $line
done < $file
26.删除文件
#!/bin/bash
echo -n "Enter filename ->"
read name
rm -i $name
27.向文件中追加内容
#!/bin/bash
echo "Before appending the file"
cat editors.txt
echo "6. NotePad++" >> editors.txt
echo "After appending the file"
cat editors.txt
28.测试文件是否存在
#!/bin/bash
filename=$1
if [ -f "$filename" ]; then
echo "File exists"
else
echo "File does not exist"
fi
29.从shell脚本发送邮件
#!/bin/bash
recipient=”admin@example.com”
subject=”Greetings”
message=”Welcome to UbuntuPit”
`mail -s $subject $recipient <<< $message`
30.解析日期和时间
#!/bin/bash
year=`date +%Y`
month=`date +%m`
day=`date +%d`
hour=`date +%H`
minute=`date +%M`
second=`date +%S`
echo `date`
echo "Current Date is: $day-$month-$year"
echo "Current Time is: $hour:$minute:$second"
31.sleep命令
#!/bin/bash
echo "How long to wait?"
read time
sleep $time
echo "Waited for $time seconds!"
32.wait命令
#!/bin/bash
echo "Testing wait command"
sleep 5 &
pid=$!
kill $pid
wait $pid
echo $pid was terminated.
33.显示最后更新的文件
#!/bin/bash
ls -lrt | grep ^- | awk 'END{print $NF}'
34.批量添加扩展名
#!/bin/bash
dir=$1
for file in `ls $1/*`
do
mv $file $file.UP
done
35.打印文件或目录数量
#!/bin/bash
if [ -d "$@" ]; then
echo "Files found: $(find "$@" -type f | wc -l)"
echo "Folders found: $(find "$@" -type d | wc -l)"
else
echo "[ERROR] Please retry with another folder."
exit 1
fi
36.清理日志文件
#!/bin/bash
LOG_DIR=/var/log
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
37.备份文件和目录
#!/bin/bash
BACKUPFILE=backup-$(date +%m-%d-%Y)
archive=${1:-$BACKUPFILE}
find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar"
echo "Directory $PWD backed up in archive file \"$archive.tar.gz\"."
exit 0
38.检查当前是不是root用户
#!/bin/bash
ROOT_UID=0
if [ "$UID" -eq "$ROOT_UID" ]
then
echo "You are root."
else
echo "你不是root"
fi
exit 0
39.从文件中删除重复的行
#! /bin/sh
echo -n "Enter Filename-> "
read filename
if [ -f "$filename" ]; then
sort $filename | uniq | tee sorted.txt
else
echo "No $filename in $pwd...try again"
fi
exit 0
上面的脚本逐行通过您的文件并删除任何重复的行。然后它将新内容放入一个新文件中,并保持原始文件不变。
40.系统升级
#!/bin/bash
echo -e "\n$(date "+%d-%m-%Y --- %T") --- 开始工作\n"
apt-get update
apt-get -y upgrade
apt-get -y autoremove
apt-get autoclean
echo -e "\n$(date "+%T") \t 脚本终止"
请把上面的脚本改写成用yum实现。
如果您是 Linux 新爱好者,雷哥强烈建议您掌握这些基本的 bash 脚本示例。
ifconfig已淘汰,ip登场
Linux 网络状态工具 ss 命令详解
这次终于搞明白VLAN技术了
除每周二、四、六定期更新的《Linux云计算一站式教程》以外,其余时间雷哥会推送一些工作中遇到的小知识、实战经验总结的文章。后续都会收录在“实战经验”合集中。